package scau.hotel.hotel_web.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import scau.hotel.hotel_service.entity.Room;
import scau.hotel.hotel_service.service.RoomService;
import scau.hotel.hotel_model.controller.RespBean;
import scau.hotel.hotel_service.vo.RoomVo;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * (Room)表控制层
 *
 * @author makejava
 * @since 2022-03-03 23:22:02
 */
@RestController
@PreAuthorize("hasAnyAuthority('admin','normal')")
@RequestMapping("room")
@Api(value = "typeApi", tags = "房间控制器")
public class RoomController {
    /**
     * 服务对象
     */
    @Resource
    private RoomService roomService;

    /**
     * 不分页查询
     *
     * @param room 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    @ApiOperation("不分页查询")
    public RespBean queryAll(Room room) {
        return RespBean.success(this.roomService.queryAll(room));
    }

//    /**
//     * 分页查询
//     *
//     * @param pageNum  起始页
//     * @param pageSize 每页数据大小
//     * @param room     筛选条件
//     * @return 查询结果
//     */
//    @GetMapping("/page")
//    @ApiOperation("分页查询")
//    public PageInfo page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Room room) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<Room> list = roomService.queryAll(room);
//        PageInfo<List> pageInfo = new PageInfo(list);
//        return pageInfo;
//    }

    /**
     * 分页查询
     *
     * @param pageNum  起始页
     * @param pageSize 每页数据大小
     * @param room     筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public RespBean page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Room room) {
        PageHelper.startPage(pageNum, pageSize);
        List<Room> list = roomService.queryAll(room);
        PageInfo<List> pageInfo = new PageInfo(list);
        return RespBean.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param roomId 主键
     * @return 单条数据
     */
    @GetMapping("{roomId}")
    @ApiOperation("通过房间id查询单条数据")
    public Room queryById(@PathVariable("roomId") Integer roomId) {
        return this.roomService.queryById(roomId);
    }

    /**
     * 新增数据
     *
     * @param roomVo 传值类
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增房间")
    @PreAuthorize("hasAuthority('admin')")
    public RespBean add(@RequestBody @Valid RoomVo roomVo) {
        return this.roomService.insert(roomVo);
    }

    /**
     * 编辑数据
     *
     * @param roomVo 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑房间数据")
    @PreAuthorize("hasAuthority('admin')")
    public RespBean edit(@RequestBody @Valid RoomVo roomVo) {
        return this.roomService.update(roomVo);
    }

    /**
     * 删除数据
     *
     * @param roomId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{roomId}")
    @ApiOperation("删除房间数据")
    @PreAuthorize("hasAuthority('admin')")
    public RespBean deleteById(@PathVariable Integer roomId) {
        return RespBean.success(this.roomService.deleteById(roomId));
    }


    /**
     * 批量删除
     *
     * @return 新增结果
     */
    @DeleteMapping("/deleteMoreRoom")
    @ApiOperation("批量删除")
    @PreAuthorize("hasAuthority('admin')")
    public RespBean deleteMoreRoom(@RequestBody List<String> arrays) {
        return RespBean.success(this.roomService.deleteMoreRoomById(arrays));
    }

    /**
     * 通过roomNo查询单条数据
     *
     * @param roomNo 主键
     * @return 实例对象
     */
    @GetMapping("/queryRoomByRoomNo/{roomNo}")
    @ApiOperation("通过房间编号查询单条数据")
    public RespBean queryRoomByRoomNo(@PathVariable("roomNo") String roomNo) {
        return RespBean.success(this.roomService.queryRoomByRoomNo(roomNo));
    }
}

